//
//  ShakeToSelectViewController.m
//  ShareYOU
//
//  Created by Suns孙泉 on 14/12/17.
//  Copyright (c) 2014年 ShareYOU. All rights reserved.
//

#import "ShakeToSelectViewController.h"
#import "Province.h"
#import "City.h"
#import "AfterShakingViewController.h"

@interface ShakeToSelectViewController ()

// 所有市数组
@property (nonatomic, retain) NSMutableArray *arr;
// 标示随机范围
@property (nonatomic, assign) int flag;
//
@property (nonatomic, retain) NSMutableArray *array;

// 华东地区
@property (nonatomic, retain) NSMutableArray *eastChinaArr;
// 华南地区
@property (nonatomic, retain) NSMutableArray *southChinaArr;
// 华中地区
@property (nonatomic, retain) NSMutableArray *middleChinaArr;
// 华北地区
@property (nonatomic, retain) NSMutableArray *northChinaArr;
// 西北地区
@property (nonatomic, retain) NSMutableArray *northWestChinaArr;
// 西南地区
@property (nonatomic, retain) NSMutableArray *southWestChinaArr;
// 东北地区
@property (nonatomic, retain) NSMutableArray *northEastChinaArr;
// 港澳台地区
@property (nonatomic, retain) NSMutableArray *disHMTArr;

@end

@implementation ShakeToSelectViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self handleData];
    }
    return self;
    
}

// 处理省市区
- (void)handleData{
    
    // 初始化数组
    self.arr = [NSMutableArray array];
    self.eastChinaArr = [NSMutableArray array];
    self.southChinaArr = [NSMutableArray array];
    self.middleChinaArr = [NSMutableArray array];
    self.northChinaArr = [NSMutableArray array];
    self.northWestChinaArr = [NSMutableArray array];
    self.northEastChinaArr = [NSMutableArray array];
    self.southWestChinaArr = [NSMutableArray array];
    self.disHMTArr = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
    NSString *sourceStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArr = [sourceStr componentsSeparatedByString:@"\n"];
    // 找到市名存入数组
    NSMutableArray *proArr = [NSMutableArray array];
    for (NSString *temp in sourceArr) {
        if (![temp hasPrefix:@" "]) {
            Province *pro = [[Province alloc] init];
            pro.name = temp;
            [proArr addObject:pro];
            [pro release];
        }
        if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]) {
            Province *pro = [proArr lastObject];
            
            temp = [[temp componentsSeparatedByString:@" "] objectAtIndex:2];
            
            if ([pro.name hasPrefix:@"山东"] || [pro.name hasPrefix:@"江苏"] || [pro.name hasPrefix:@"安徽"] || [pro.name hasPrefix:@"浙江"] || [pro.name hasPrefix:@"福建"] || [pro.name hasPrefix:@"上海"]) {
                
                [self.eastChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"广东"] || [pro.name hasPrefix:@"广西"] || [pro.name hasPrefix:@"海南"]){
                
                [self.southChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"湖北"] || [pro.name hasPrefix:@"湖南"] || [pro.name hasPrefix:@"河南"] || [pro.name hasPrefix:@"江西"]){
                
                [self.middleChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"北京"] || [pro.name hasPrefix:@"天津"] || [pro.name hasPrefix:@"河北"] || [pro.name hasPrefix:@"山西"] || [pro.name hasPrefix:@"内蒙古"]){
                
                [self.northChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"宁夏"] || [pro.name hasPrefix:@"新疆"] || [pro.name hasPrefix:@"青海"] || [pro.name hasPrefix:@"陕西"] || [pro.name hasPrefix:@"甘肃"]){
                
                [self.northWestChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"四川"] || [pro.name hasPrefix:@"云南"] || [pro.name hasPrefix:@"贵州"] || [pro.name hasPrefix:@"西藏"] || [pro.name hasPrefix:@"重庆"]){
                
                [self.southWestChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"辽宁"] || [pro.name hasPrefix:@"吉林"] || [pro.name hasPrefix:@"黑龙江"]){
                
                [self.northEastChinaArr addObject:temp];
                
            }else if ([pro.name hasPrefix:@"台湾"] || [pro.name hasPrefix:@"香港"] || [pro.name hasPrefix:@"澳门"]){
                
                [self.disHMTArr addObject:temp];
                
            }
            [pro.district addObject:temp];
            [self.arr addObject:temp];
        }
    }
}

#pragma mark -
#pragma mark 摇一摇
- (BOOL)canBecomeFirstResponder{
    
    return YES;// default is NO
    
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    NSLog(@"开始摇动手机");
    
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    NSLog(@"stop");
    AfterShakingViewController *afterSVC = [[AfterShakingViewController alloc] init];
    switch (self.flag) {
        case 0:
            self.array = [NSMutableArray arrayWithArray:self.arr];
            break;
        case 1:
            self.array = [NSMutableArray arrayWithArray:self.southChinaArr];
            break;
        case 2:
            self.array = [NSMutableArray arrayWithArray:self.southWestChinaArr];
            break;
        case 3:
            self.array = [NSMutableArray arrayWithArray:self.eastChinaArr];
            break;
        case 4:
            self.array = [NSMutableArray arrayWithArray:self.northChinaArr];
            break;
        case 5:
            self.array = [NSMutableArray arrayWithArray:self.northWestChinaArr];
            break;
        case 6:
            self.array = [NSMutableArray arrayWithArray:self.northEastChinaArr];
            break;
        case 7:
            self.array = [NSMutableArray arrayWithArray:self.middleChinaArr];
            break;
        case 8:
            self.array = [NSMutableArray arrayWithArray:self.disHMTArr];
            break;
        default:
            break;
    }
    
    // 摇动随机一个城市
    int randomNum = arc4random() % self.array.count;
//    NSLog(@"%d", randomNum);
    afterSVC.cityName = [self.array objectAtIndex:randomNum];
    NSLog(@"%@", [afterSVC.cityName description]);
    [self.navigationController pushViewController:afterSVC animated:NO];
    
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    NSLog(@"取消");
    
}

- (void)dealloc
{
    [_arr release];
    [_array release];
    [_southChinaArr release];
    [_southWestChinaArr release];
    [_eastChinaArr release];
    [_northChinaArr release];
    [_northEastChinaArr release];
    [_northWestChinaArr release];
    [_middleChinaArr release];
    [_disHMTArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
    self.flag = 0;
    
    UIButton *southChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    southChinaButton.frame = CGRectMake(80, 80, 80, 80);
    [southChinaButton setTitle:@"华南地区" forState:UIControlStateNormal];
    [southChinaButton addTarget:self action:@selector(southChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:southChinaButton];
    
    UIButton *southWestChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    southWestChinaButton.frame = CGRectMake(215, 80, 80, 80);
    [southWestChinaButton setTitle:@"西南地区" forState:UIControlStateNormal];
    [southWestChinaButton addTarget:self action:@selector(southWestChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:southWestChinaButton];
    
    UIButton *eastChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    eastChinaButton.frame = CGRectMake(80, 200, 80, 80);
    [eastChinaButton setTitle:@"华东地区" forState:UIControlStateNormal];
    [eastChinaButton addTarget:self action:@selector(eastChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:eastChinaButton];
    
    UIButton *northChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    northChinaButton.frame = CGRectMake(215, 200, 80, 80);
    [northChinaButton setTitle:@"华北地区" forState:UIControlStateNormal];
    [northChinaButton addTarget:self action:@selector(northChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:northChinaButton];
    
    UIButton *northWestChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    northWestChinaButton.frame = CGRectMake(80, 320, 80, 80);
    [northWestChinaButton setTitle:@"西北地区" forState:UIControlStateNormal];
    [northWestChinaButton addTarget:self action:@selector(northWestChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:northWestChinaButton];
    
    UIButton *northEastChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    northEastChinaButton.frame = CGRectMake(215, 320, 80, 80);
    [northEastChinaButton setTitle:@"东北地区" forState:UIControlStateNormal];
    [northEastChinaButton addTarget:self action:@selector(northEastChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:northEastChinaButton];
    
    UIButton *middleChinaButton = [UIButton buttonWithType:UIButtonTypeSystem];
    middleChinaButton.frame = CGRectMake(80, 440, 80, 80);
    [middleChinaButton setTitle:@"华中地区" forState:UIControlStateNormal];
    [middleChinaButton addTarget:self action:@selector(middleChinaButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:middleChinaButton];
    
    UIButton *disHMTButton = [UIButton buttonWithType:UIButtonTypeSystem];
    disHMTButton.frame = CGRectMake(215, 440, 80, 80);
    [disHMTButton setTitle:@"港澳台地区" forState:UIControlStateNormal];
    [disHMTButton addTarget:self action:@selector(disHMTButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:disHMTButton];
    
}

- (void)southChinaButtonClicked:(UIButton *)button{
    
    self.flag = 1;
    
}

- (void)southWestChinaButtonClicked:(UIButton *)button{
    
    self.flag = 2;
    
}

- (void)eastChinaButtonClicked:(UIButton *)button{
    
    self.flag = 3;
    
}

- (void)northChinaButtonClicked:(UIButton *)button{
    
    self.flag = 4;
    
}

- (void)northWestChinaButtonClicked:(UIButton *)button{
    
    self.flag = 5;
    
}

- (void)northEastChinaButtonClicked:(UIButton *)button{
 
    self.flag = 6;
    
}

- (void)middleChinaButtonClicked:(UIButton *)button{
    
    self.flag = 7;
    
}

- (void)disHMTButtonClicked:(UIButton *)button{
 
    self.flag = 8;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
